package com.example.cupid.controller;

import com.example.cupid.pojo.Wechat;
import com.example.cupid.service.Impl.WechatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 2021/9/3
 **/
@Controller
@RequestMapping("/wechat")
public class WechatController {

    @Autowired
    private WechatServiceImpl wechatService;

    @GetMapping("/manage")
    public String toWechatManage(@RequestParam int id, Model model){
        List<Wechat> saveWechats = wechatService.getSaveWechatByUserId(id);
        List<Wechat> takeWechats = wechatService.getTakeWechatByUserId(id);
        model.addAttribute("saveWechats",saveWechats);
        model.addAttribute("takeWechats",takeWechats);
        return "wechat_manage";
    }
//    投放微信
    @PostMapping
    @ResponseBody
    public boolean saveWechat(Wechat wechat,Integer userId){
        Wechat existWechat = wechatService.getWechat(wechat.getWechat());
        if(existWechat!=null){
            return false;
        }
//        将投放的微信添加到微信表中
        boolean b = wechatService.saveWechat(wechat);
        Integer wechatId = wechatService.getWechatIdByWechatName(wechat.getWechat());
//        将投放的微信存放到用户微信中间表
        boolean b1 = wechatService.saveUserWechatSave(userId, wechatId);
        return b&&b1;
    }
    @GetMapping("/success")
    public String toSuccess(){
        return "success";
    }
    //获取微信
   /* @PostMapping
    @RequestMapping
    public boolean takeWechat(Integer userId){

    }
*/

}
